package wish;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import control.ServerIp;

public class WishInsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public WishInsertServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		ServerIp ip = new ServerIp();
		
		Connection conn = null;
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rscount = null;
		ResultSet check = null;
		//ResultSet rs = null;
		
		String getGS_CODE = request.getParameter("GS_CODE");
		Integer getMB_NO = Integer.parseInt(request.getParameter("MB_NO"));
	
		try{
			int gw_no = 0;
			//Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName(ip.getDriver());
			conn = DriverManager.getConnection(ip.getUrl(), ip.getId(), ip.getPw());
			stmt = conn.createStatement();
			
			String checkSql = "select * from goods_wish where mb_no=" + getMB_NO + " and gs_code='"+getGS_CODE+"'";
			check = stmt.executeQuery(checkSql);
			if(!check.next())
			{
				String countSql = "select max(gw_no) from goods_wish";
				rscount = stmt.executeQuery(countSql);
				if(rscount.next()) gw_no = rscount.getInt(1);
				gw_no = gw_no + 1;
				
				pstmt = conn.prepareStatement("insert into goods_wish values(?,?,?)");
				pstmt.setInt(1, gw_no);
				pstmt.setString(2, getGS_CODE);
				pstmt.setInt(3, getMB_NO);
				pstmt.executeUpdate();
				
				//String insertSql = "insert into goods_wish values("+gw_no+",'"+getGS_CODE+"',"+getMB_NO+")";
				//rs = stmt.executeQuery(insertSql);
				try{
					stmt.close();
					conn.close();
					//rs.close();
					pstmt.close();
				}catch(Exception e){}
				response.sendRedirect("/Dmyna/myshop/wish/wishTrueResult.jsp");
			}else{
				try{
					stmt.close();
					conn.close();
				}catch(Exception e){}
				response.sendRedirect("/Dmyna/myshop/wish/wishFalseResult.jsp");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}		
}